package com.cmccgz.udacs_landlord_webapp.main.peoplemanager;

import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.CardAdapter;
import com.cmccgz.udacs_landlord_webapp.adapter.CardTypeAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.CardBean;
import com.cmccgz.udacs_landlord_webapp.bean.CardMsg;
import com.cmccgz.udacs_landlord_webapp.bean.RenterBean;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.contract.AddContract;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.presenter.AddPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

import java.util.ArrayList;

/**
 * @author Denis
 * @ClassName: DelayCardActivity
 * @Description: 延迟租客房卡有效期页面
 * @date 2017/6/8 10:05
 */

public class AddCardActivity extends BaseActivity implements AddContract.View, View.OnClickListener {
    private ViewHelper mViewHelper;
    private TextView tvName, tvNumber, tvTime, tv_noCard;
    private RelativeLayout rlTime;
    private RenterBean renterBean;
    private RecyclerView rv_add;
    private RelativeLayout item0;
    private AddContract.Presenter addPresenter;
    private CardAdapter cardAdapter;
    private AlertDialog addCardDialog;
    ArrayList<CardMsg.CardAllType> card_all_type;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_add_card;
    }

    @Override
    protected void initView() {
        item0 = (RelativeLayout) findViewById(R.id.item0);
        rv_add = (RecyclerView) findViewById(R.id.rv_add);
        tv_noCard = (TextView) findViewById(R.id.tv_noCard);
        renterBean = (RenterBean) getIntent().getSerializableExtra("renterBean");
        tvName = (TextView) findViewById(R.id.tv_name);
        tvNumber = (TextView) findViewById(R.id.tv_number);
        tvTime = (TextView) findViewById(R.id.tv_time);
        rlTime = (RelativeLayout) findViewById(R.id.rl_time);
        item0.setOnClickListener(this);
        rlTime.setOnClickListener(this);
        tvName.setText(renterBean.user_name);
        tvNumber.setText(renterBean.house_name);
        tvTime.setText(renterBean.house_end_time);
        VirtualLayoutManager lm = new VirtualLayoutManager(this);
        rv_add.setLayoutManager(lm);
        DelegateAdapter adapter = new DelegateAdapter(lm);
        cardAdapter = new CardAdapter(this, new LinearLayoutHelper(2), null);
        adapter.addAdapter(cardAdapter);
        rv_add.setAdapter(adapter);
        rv_add.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        setNoCard(true);
        new AddPresenter(this);
        addPresenter.getCardMsg(renterBean.user_mobile, renterBean.house_id);
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {
        mViewHelper = ViewHelper.instance(mContext);
        if (mViewHelper != null) {
            mViewHelper.setTitle("新增卡片");
            mViewHelper.setBackTextVisable(true);
        }
    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }


    @Override
    public void setPresenter(AddContract.Presenter presenter) {
        this.addPresenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_time:
                break;
            case R.id.tv_right:
                ToastUtils.show(mContext, "完成");
                break;
            case R.id.tv_back:
                AddCardActivity.this.finish();
                break;
            case R.id.item0:
                showAddDialog();
                break;
        }
    }


    @Override
    public void loadingData() {
        showLoadingDialog("正在努力加载...");
    }

    @Override
    public void finishGetData(CardMsg resp) {
        dismissLoadingDialog();
        cardAdapter.setDatas(resp.card_detail);
        card_all_type = resp.card_all_type;
        setNoCard(cardAdapter.getItemCount() == 0);
        cardAdapter.notifyDataSetChanged();
    }

    @Override
    public void finishUploadData(String result) {
        dismissLoadingDialog();
        if (result.equals("0")) {
            ToastUtils.show(mContext, "新增成功");
            addPresenter.getCardMsg(renterBean.user_mobile, renterBean.house_id);
            if (addCardDialog != null && addCardDialog.isShowing()) {
                addCardDialog.dismiss();
            }
        } else {
            ToastUtils.show(mContext, "新增失败");
        }

    }

    private void setNoCard(boolean isNoCard) {
        if (isNoCard) {
            tv_noCard.setVisibility(View.VISIBLE);
            rv_add.setVisibility(View.GONE);
        } else {
            tv_noCard.setVisibility(View.GONE);
            rv_add.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 弹出添加卡片对话框
     */
    private void showAddDialog() {
        if (addCardDialog == null) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.BottomDialog);
            builder.setCancelable(true);
            View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_card, null);
            builder.setView(view);
            final Spinner sp_type = (Spinner) view.findViewById(R.id.sp_type);
            final EditText et_num = (EditText) view.findViewById(R.id.et_num);
            final DatePicker datePicker = (DatePicker) view.findViewById(R.id.datePicker);
            Button bn_cancal = (Button) view.findViewById(R.id.bn_cancal);
            Button bn_ok = (Button) view.findViewById(R.id.bn_ok);
            bn_cancal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (addCardDialog != null && addCardDialog.isShowing()) {
                        addCardDialog.dismiss();
                    }
                }
            });
            bn_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String etNum = et_num.getText().toString();
                    if (TextUtils.isEmpty(etNum)) {
                        ToastUtils.show(mContext, "卡号不能为空");
                        return;
                    }
                    String tvTime = datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker.getDayOfMonth();
                    CardMsg.CardAllType selectedItem = (CardMsg.CardAllType) sp_type.getSelectedItem();
                    ArrayList<CardBean> cardBeens = new ArrayList<>();
                    CardBean cardBean = new CardBean();
                    cardBean.card_id = etNum;
                    cardBean.card_type = selectedItem.type_id;
                    cardBean.card_end_time = tvTime;
                    cardBeens.add(cardBean);
                    addPresenter.addCard(renterBean.user_mobile, renterBean.house_id, cardBeens);
                }
            });
            sp_type.setAdapter(new CardTypeAdapter(mContext, card_all_type));
            addCardDialog = builder.create();
            Window window = addCardDialog.getWindow();
            window.getDecorView().setPadding(0, 0, 0, 0);
            android.view.WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
        }
        addCardDialog.show();
    }
}
